package cdu.sl.service.impl;

import cdu.sl.mapper.StudentMapper;
import cdu.sl.pojo.PageResult;
import cdu.sl.pojo.StuQueryParam;
import cdu.sl.pojo.Student;
import cdu.sl.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {


    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页查询学生数据
     *
     * @param stuQueryParam
     * @return
     */
    @Override
    public PageResult<Student> page(StuQueryParam stuQueryParam) {
        // 1.设置分页参数
        PageHelper.startPage(stuQueryParam.getPage(), stuQueryParam.getPageSize());

        // 2.获取查询结果
        List<Student> stuList = studentMapper.list(stuQueryParam);

        // 3.解析查询结果并返回
        Page<Student> p = (Page<Student>) stuList;
        return new PageResult<Student>(p.getTotal(), p.getResult());
    }

    /**
     * 批量删除员工信息
     */
    @Override
    public void delete(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    /**
     * 新增学生
     *
     * @param student
     */
    @Override
    public void add(Student student) {
        studentMapper.insert(student);
    }

    /**
     * 根据id获取学生信息
     *
     * @param id
     * @return
     */
    @Override
    public Student getInfo(Integer id) {
        return studentMapper.getById(id);
    }

    /**
     * 修改学生信息
     *
     * @param student
     */
    @Override
    public void update(Student student) {
        studentMapper.update(student);
    }

    /**
     * 根据学生id扣除对应分数
     *
     * @param id
     * @param score
     */
    @Override
    public void updateViolationInfo(Integer id, Integer score) {
          studentMapper.updateViolationInfoById(id,score);
    }
}
